﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _1_4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ButtonTiJiao_Click(object sender, EventArgs e)
    {
        TextBox[] tbxCPMC = new TextBox[] { this.TextBoxCPMC1,this.TextBoxCPMC2,this.TextBoxCPMC3,this.TextBoxCPMC4,this.TextBoxCPMC5 };
        TextBox[] tbxJLDW = new TextBox[] { this.TextBoxJLDW1, this.TextBoxJLDW2, this.TextBoxJLDW3, this.TextBoxJLDW4, this.TextBoxJLDW5 };
        TextBox[] tbxFQPFL = new TextBox[] { this.TextBoxFQPFL1, this.TextBoxFQPFL2, this.TextBoxFQPFL3, this.TextBoxFQPFL4, this.TextBoxFQPFL5 };
        TextBox[] tbxWS_WRWMC = new TextBox[] { this.TextBoxWS_WRWMC1, this.TextBoxWS_WRWMC2, this.TextBoxWS_WRWMC3, this.TextBoxWS_WRWMC4, this.TextBoxWS_WRWMC5,this.TextBoxWS_WRWMC6,this.TextBoxWS_WRWMC7,this.TextBoxWS_WRWMC8,this.TextBoxWS_WRWMC9,this.TextBoxWS_WRWPFL10,this.TextBoxWS_WRWPFL11,this.TextBoxWS_WRWPFL12,this.TextBoxWS_WRWPFL13,this.TextBoxWS_WRWPFL14,this.TextBoxWS_WRWPFL15 };
        TextBox[] tbxWS_WRWPFL = new TextBox[] { this.TextBoxWS_WRWPFL1, this.TextBoxWS_WRWPFL2, this.TextBoxWS_WRWPFL3, this.TextBoxWS_WRWPFL4, this.TextBoxWS_WRWPFL5, this.TextBoxWS_WRWPFL6, this.TextBoxWS_WRWPFL7, this.TextBoxWS_WRWPFL8, this.TextBoxWS_WRWPFL9, this.TextBoxWS_WRWPFL10, this.TextBoxWS_WRWPFL11, this.TextBoxWS_WRWPFL12, this.TextBoxWS_WRWPFL13, this.TextBoxWS_WRWPFL14, this.TextBoxWS_WRWPFL15 };
        TextBox[] tbxFQ_WRWMC = new TextBox[] { this.TextBoxFQ_WRWMC1, this.TextBoxFQ_WRWMC2, this.TextBoxFQ_WRWMC3, this.TextBoxFQ_WRWMC4, this.TextBoxFQ_WRWMC5, this.TextBoxFQ_WRWMC6, this.TextBoxFQ_WRWMC7, this.TextBoxFQ_WRWMC8, this.TextBoxFQ_WRWMC9, this.TextBoxWS_WRWPFL10, this.TextBoxWS_WRWPFL11, this.TextBoxWS_WRWPFL12, this.TextBoxWS_WRWPFL13, this.TextBoxWS_WRWPFL14, this.TextBoxWS_WRWPFL15 };
        TextBox[] tbxFQ_WRWPFL = new TextBox[] { this.TextBoxFQ_WRWPFL1, this.TextBoxFQ_WRWPFL2, this.TextBoxFQ_WRWPFL3, this.TextBoxFQ_WRWPFL4, this.TextBoxFQ_WRWPFL5, this.TextBoxFQ_WRWPFL6, this.TextBoxFQ_WRWPFL7, this.TextBoxFQ_WRWPFL8, this.TextBoxFQ_WRWPFL9, this.TextBoxWS_WRWPFL10, this.TextBoxWS_WRWPFL11, this.TextBoxWS_WRWPFL12, this.TextBoxWS_WRWPFL13, this.TextBoxWS_WRWPFL14, this.TextBoxWS_WRWPFL15 };

        if (true)
        {
            
        }

        string strCuoWu = "提交内容有错误：";//记录错误内容
        int CuoWuXuHao = 0;//错误序号
        bool TuiChu = false; //记录逻辑判断过程中是否有错误需要中断退出提交过程

        using (DataClassesDataContext mydata = new DataClassesDataContext())
        {
            //所有产品年产量总和，这里暂时没考虑各个产品计量单位不同的情况
            double? nianchanliang = (from a in mydata.a01103
                                     where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                                     where a.ShenBaoNianDu == Convert.ToString(Session["ShenBaoNianDu"])
                                     select a.PRODUCT_COUNT).Sum();
            if (nianchanliang==null || nianchanliang==0)
            {
                CuoWuXuHao++;
                strCuoWu = strCuoWu + Convert.ToString(CuoWuXuHao) + "、" + "表1-3里还没填写产品产量，无法生产本表数据，请先填写表1-3。" + (char)13 ;
                TuiChu = true;
            }

            //表1-7的污水排放量，单位是（万吨）
            double? wushuipaifangliang = (from a in mydata.a01107_1
                                          where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                                          where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                                          select a.YEAR_SUMCOUNT).FirstOrDefault();
            
            
            //表1-8的废气排放量，单位是（万标立米）
            double? feiqipaifangliang = (from a in mydata.a01108_1
                                          where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                                          where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                                          select a.GAS_CNT).FirstOrDefault();

            //产品名称
            var rdcp = from a in mydata.a01103
                       where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                       where a.ShenBaoNianDu == Convert.ToString(Session["ShenBaoNianDu"])
                       select new { a.PRODUCT_NAME, a.PRODUCT_UNIT };

            //表1-7的污染物排放量，单位是（吨）
            var rdws = (from a in mydata.a01107_2
                        join c in mydata.CODE_POLLUTION_TABLE on a.CODE equals c.CODE
                        where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                        where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                        orderby a.YEAR_SUMCOUNT descending
                        select new
                        {
                            code = a.CODE,
                            name = c.NAME,
                            pfl = a.YEAR_SUMCOUNT
                        }).Take(3);

            //表1-8的污染物排放量，单位是（吨）
            var rdfq = (from a in mydata.a01108_2
                        join c in mydata.CODE_POLLUTION_TABLE on a.CODE equals c.CODE
                        where a.qiyeid == Convert.ToInt32(Session["qiyeid"])
                        where a.DATA_MONTH == Convert.ToString(Session["ShenBaoNianDu"])
                        orderby a.YEAR_SUMCOUNT descending
                        select new 
                        { 
                            code=a.CODE,
                            name=c.NAME,
                            pfl=a.YEAR_SUMCOUNT
                        }).Take(3);

            if ((wushuipaifangliang==null || wushuipaifangliang==0) && (feiqipaifangliang==null || feiqipaifangliang==0))
            {
                CuoWuXuHao++;
                strCuoWu = strCuoWu + Convert.ToString(CuoWuXuHao) + "、" + "表1-7的污水排放量和表1-8的废气排放量都还没有数据，请先去填写。" + (char)13;
                TuiChu = true;
            }
            
            if (nianchanliang!=null && nianchanliang!=0)
            {
                int xh1 = 0;
                int xh2 = 0;
                foreach (var rcp in rdcp)
                {
                    tbxCPMC[xh1].Text = rcp.PRODUCT_NAME;
                    tbxJLDW[xh1].Text = rcp.PRODUCT_UNIT;
                    xh2 = xh1*3+1;
                    foreach (var rws in rdws)
                    {
                        tbxWS_WRWMC[xh2].Text = rws.name.ToString();
                        tbxWS_WRWPFL[xh2].Text = Convert.ToString((rws.pfl*1000)/nianchanliang);
                    }
                    foreach (var rfq in rdfq)
                    {
                        tbxFQ_WRWMC[xh2].Text = rfq.name.ToString();
                        tbxFQ_WRWPFL[xh2].Text = Convert.ToString((rfq.pfl * 1000) / nianchanliang);
                    }
                    xh1++;
                }
            }
        }

        LabelTiShi.Visible = false;
        if (TuiChu==true)
        {
            MessageBox("tishi", "生成数据失败！");
            TextBoxCuoWu.Text = strCuoWu;
        }
        else
        {
            MessageBox("tishi", "生成数据成功!");
        }
    }

    //// <summary> 
    /// 弹出提示信息框 
    /// </summary> 
    /// <param name="strKey">函数key值</param> 
    /// <param name="strInfo">提示信息</param> 
    public void MessageBox(string strKey, string strInfo)
    {
        if (!ClientScript.IsClientScriptBlockRegistered(strKey))
        {
            string strjs = "alert('" + strInfo + "');";
            ClientScript.RegisterClientScriptBlock(this.GetType(), strKey, strjs, true);
        }
    } 
}